<template>
  <div class="mod-config">

    <el-row :gutter="20">
      <el-col :span="4" :xs="24" >
        <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
          <el-form-item style="right: auto">
            <el-button v-if="isAuth('sys:dictmanager:save')" type="primary" @click="addOrUpdateHandle()">新增字典</el-button>
          </el-form-item>
        </el-form>
        <el-tree
          :data="dataList"
          :props="props"
          :expand-on-click-node="false"
          ref="tree"
          default-expand-all
          :highlight-current="true"
          @node-click="handleNodeClick"
        >
           <span class="custom-tree-node" slot-scope="{ node, data }">
        <span>{{ node.label }}</span>
        <span>
          <el-button
            type="text"
            size="mini"
            @click.stop =" addOrUpdateHandle(data.id)">
            编辑
          </el-button>
          <el-button
            type="text"
            size="mini"
            @click.stop="dictDelete(data.id)">
            删除
          </el-button>
        </span>
      </span>

</el-tree>
      </el-col>
      <el-col  :span="20" :xs="24">
        <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">

          <el-form-item>
            <el-button v-if="isAuth('sys:dictmanager:save')&&this.dictCode!=''" type="primary" @click="addOrUpdateDictDataHandle()">新增字典数据</el-button>
            <el-button v-if="isAuth('sys:dictmanager:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
          </el-form-item>
        </el-form>
        <el-table
          :data="dictDataList"
          border
          v-loading="dataListLoading"
          @selection-change="selectionChangeHandle"
          style="width: 100%;">
          <el-table-column
            type="selection"
            header-align="center"
            align="center"
            width="50">
          </el-table-column>
          <el-table-column
            prop="dictData"
            header-align="center"
            align="center"
            label="字典值">
          </el-table-column>
          <el-table-column
            prop="dictText"
            header-align="center"
            align="center"
            label="字典文本">
          </el-table-column>
          <el-table-column
            prop="dictCode"
            header-align="center"
            align="center"
            label="字典编码">
          </el-table-column>
          <el-table-column
            fixed="right"
            header-align="center"
            align="center"
            width="150"
            label="操作">
            <template slot-scope="scope">
              <el-button type="text" size="small" @click="addOrUpdateDictDataHandle(scope.row.id)">修改</el-button>
              <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

      </el-col>
    </el-row>


    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    <dict-data-add-or-update v-if="DictDataAddOrUpdateVisible" ref="addOrUpdateDictData" @refreshDataList="getDictDataList(dictCode)"></dict-data-add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './dictmanager-add-or-update'
  import DictDataAddOrUpdate from './dictdatamanager-add-or-update'
  import { treeDataTranslate } from '@/utils'
  export default {
    data () {
      return {
        dataForm: {
          key: ''
        },
        props:{
          label:"dictName"
        },
        dictCode:'',
        dataList: [],
        dictDataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        DictDataAddOrUpdateVisible:false
      }
    },
    components: {
      AddOrUpdate,
      DictDataAddOrUpdate
    },
    activated () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/sys/dictmanager/list'),
          method: 'get',
          params: this.$http.adornParams({ })
        }).then(({data}) => {
          console.log(data,"data重新进了吗")
          this.dataList = treeDataTranslate(data, 'dictCode')
          this.dataListLoading = false
          this.dataListLoading = false
        })
      },
      getDictDataList (dictCode) {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/sys/dictdatamanager/list'),
          method: 'get',
          params: this.$http.adornParams({
            'dictCode': dictCode
          })
        }).then(({data}) => {
          this.dictDataList =data.data
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 节点单击事件
      handleNodeClick(data) {
        console.log(data);
        this.dictCode = data.dictCode
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      addOrUpdateDictDataHandle(id,dictCode){
        this.DictDataAddOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdateDictData.init(id,this.dictCode)
        })
      },
      // 删除
      dictDelete (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/sys/dictmanager/delete'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                  this.getDictDataList();
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      },


      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/sys/dictdatamanager/delete'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      }
    }
  }
</script>
<style>
  .custom-tree-node {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 14px;
    padding-right: 8px;
  }
</style>
